package Euler55;

import java.math.BigInteger;

public class LychrelNumbers {
	public static void main(String[] args) {
		int sum = 0;
		
		for (int i = 0; i<10000; i++)
		{
			if(isLychrel(new BigInteger(Integer.toString(i))))
			{
				System.out.println(i);
				sum++;
			}
		}
		
		System.out.println(sum);
		//System.out.println(isLychrel(new BigInteger("196")));
	}

	private static boolean isPalindrome(BigInteger number) {
        if(number.equals(reverse(number))){
            return true;
        }
        return false;
    }
 
    private static BigInteger reverse(BigInteger number){
    	BigInteger reverse = new BigInteger("0");
     
        while(!number.equals(new BigInteger("0"))){
          reverse = reverse.multiply(new BigInteger("10")).add(number.mod(new BigInteger("10")));
          number = number.divide(new BigInteger("10"));
        }
             
        return reverse;
    }
    
    public static boolean isLychrel(BigInteger num)
    {
    	BigInteger curr = num;
    	
    	for(int i = 0; i < 50; i++)
    	{
    		BigInteger reverse = reverse(curr);
    		BigInteger sum = curr.add(reverse);
    		
    		if(isPalindrome(sum))
    		{
    			return false;
    		}
    		curr = sum;
    	}
    	
    	return true;
    }
	
}
